//
//  ImagePreviewModule.m
//  RemarryApp
//
//  Created by admin on 2020/6/27.
//

#import "ImagePreviewModule.h"
#import <React/RCTLog.h>
#import "TZImagePickerController.h"
#import "AppDelegate.h"

@implementation ImagePreviewModule

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(downloadImage:(NSString *)imageUrl)
{
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{
      // 拍照
      UIImagePickerController* controller = [[UIImagePickerController alloc] init];
//      controller.delegate = self;
      // 拍照
      if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] ) {
          controller.sourceType = UIImagePickerControllerSourceTypeCamera;
          controller.showsCameraControls = YES;
      } else {
          controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
      }
      controller.allowsEditing = YES;
      [[self getCurrentNavigationController] presentViewController: controller
                                                          animated: YES
                                                        completion: NULL];
  }];
}

RCT_REMAP_METHOD(select,maxCount:(int)maxCount resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    [[NSOperationQueue mainQueue]addOperationWithBlock:^{
      TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxCount delegate:nil];
      
      // You can get the photos by block, the same as by delegate.
      // 你可以通过block或者代理，来得到用户选择的照片.
      [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if(assets.count > 0) {
            NSArray *tempArray = [NSArray arrayWithArray:assets];
            NSMutableArray *arrList = [[NSMutableArray alloc] init];
            for (int i = 0 ; i< tempArray.count; i++) {
                PHAsset *asset=tempArray[i];
                PHImageManager * imageManager = [PHImageManager defaultManager];
              [imageManager requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
                NSString *str = [NSString stringWithFormat:@"assets-library://asset/asset.%@?id=%@&ext=%@",dataUTI,[asset.localIdentifier substringToIndex:36],dataUTI];
                NSLog(@"str:%@",str);
                [arrList addObject:str];
                if (arrList.count == assets.count) {
                    resolve(arrList);
                }
              }];
            }
        }
      }];
      
      imagePickerVc.naviBgColor = [UIColor whiteColor];
      
      [[self getCurrentNavigationController] presentViewController:imagePickerVc animated:YES completion:nil];
    }];
}

- (UINavigationController *)getCurrentNavigationController {
    id rootViewController = [AppDelegate getRootViewController];
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        return rootViewController;
    } else if ([rootViewController isKindOfClass:[UIViewController class]]) {
        return ((UIViewController *) rootViewController).navigationController;
    }
//    if (rootViewController) {
//
//        if ([rootViewController isKindOfClass:[MainViewController class]]) {
//            rootViewController = ((MainViewController *) rootViewController).selectedViewController;
//        }
//
//        if ([rootViewController isKindOfClass:[UINavigationController class]]) {
//            return rootViewController;
//        } else if ([rootViewController isKindOfClass:[UIViewController class]]) {
//            return ((UIViewController *) rootViewController).navigationController;
//        }
//    }
    return nil;
}

@end
